﻿using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Telephones
{
    public class PageEditTelephoneModel : AbstractPageEditModel
    {
        public Telephone Telephone { get; set; }

        public override void InnerLoad()
        {
            if (!string.IsNullOrEmpty(this.Fragment))
            {
                // Récupération des Ids
                int idTelephone = 0;
                int.TryParse(this.DicFragments["IdTelephone"], out idTelephone);

                int idContact = 0;
                if (this.DicFragments.ContainsKey("IdContact"))
                {
                    int.TryParse(this.DicFragments["IdContact"], out idContact);
                }

                // Chargement ou création de l'entité
                if (idTelephone > 0)
                {
                    this.Telephone = this._pageContext
                                        .Telephones
                                        .Where(tel => tel.Id == idTelephone)
                                        .FirstOrDefault();
                }
                else
                {
                    this.Telephone = new Telephone();
                    this.Telephone.IdContact = idContact;
                    this._pageContext.Telephones.Add(this.Telephone);
                }
            }
        }
    }
}
